package com.jzo2o.foundations.controller.operation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 区域服务
 * @author L
 */
@RestController("operationServeController")
@RequestMapping("/operation/serve")
@Api(tags = "区域服务相关接口")
public class ServeController {

    @Resource
    private IServeService serveService;



    @GetMapping("/page")
    @ApiOperation("区域服务分页查询")
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        return serveService.page(servePageQueryReqDTO);
    }

    @GetMapping("/add")
    @ApiOperation("区域服务新增")
    public void serveAdd(@RequestBody List<ServeUpsertReqDTO> dto){
       serveService.serveAdd(dto);
    }

    /**
     * 根据服务id修改区域价格
     * @param price 要修改的价格
     */
    @PutMapping("/{id}")
    @ApiOperation("修改区域价格")
    public void updateServePrice(@PathVariable Long id, @RequestParam String price) {
        if (Objects.isNull(id)||Objects.isNull(price)|| price.isEmpty()){
            return;
        }
        Serve serve = new Serve();
        serve.setPrice(new BigDecimal(price));
        serveService.update(serve, new LambdaQueryWrapper<Serve>().eq(Serve::getId, id));
    }

    @PutMapping("/onSale/{id}")
    @ApiOperation("修改服务状态为上架")
    public void updateShelves(@PathVariable Long id){
        if (Objects.isNull(id)){
            return;
        }
        serveService.updateShelves(id);
    }

    /**
     * 删除区域服务
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除区域服务")
    public  void deleteServe(@PathVariable Long id){
        if (Objects.isNull(id)){
            throw  new ForbiddenOperationException("参数不能为空");
        }
        serveService.deleteServe(id);
    }

    /**
     * 下架区域服务
     * @param id 要下架服务的id
     */
    @PutMapping("/offSale/{id}")
    @ApiOperation("下架区域服务")
    public void takenOffTheShelves(@PathVariable Long id){
        if (Objects.isNull(id)){
            throw new ForbiddenOperationException("参数不能为空");
        }
        serveService.takenOffTheShelves(id);
    }


    @PutMapping("/onHot/{id}")
    @ApiOperation("设置热门")
    public void onHo(@PathVariable Long id){
        if (Objects.isNull(id)){
            throw new ForbiddenOperationException("参数不能为空");
        }
        serveService.onHo(id);
    }



}
